﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BenhVien
{
    public partial class ThemVT : Form
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        Connect conn = new Connect();
        public ThemVT()
        {
            InitializeComponent();
        }
        public void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from KhoVatTu";
            da.SelectCommand = command;
            da.Fill(dt);
            dataGridView1.DataSource = dt;

        }
        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            if (txtMaVT.Text == "" | txtTenVT.Text == "" | txtMT.Text == "" | txtSL.Text == "" | txtGT.Text == "")
                MessageBox.Show("Bạn Chưa Nhập Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK);
            else
            {
                    DataRow row = dt.NewRow();
                    row["MaVT"] = txtMaVT.Text;
                    row["Ten"] = txtTenVT.Text;
                    row["MoTa"] = txtMT.Text;
                    row["SoLuongTrongKho"] = txtSL.Text;
                    row["GiaTien"] = txtGT.Text;
                    dt.Rows.Add(row);
                    SqlCommand commandInsert = new SqlCommand();
                    commandInsert.Connection = conn.con;
                    commandInsert.CommandType = CommandType.Text;
                    commandInsert.CommandText = @"Insert KhoVatTu (MaVT, Ten, MoTa, SoLuongTrongKho,GiaTien) Values 
                                                        (@MaVT, @Ten, @MoTa, @SoLuongTrongKho, @GiaTien)";
                    commandInsert.Parameters.Add("@MaVT", SqlDbType.NVarChar, 50, "MaVT");
                    commandInsert.Parameters.Add("@Ten", SqlDbType.NVarChar, 50, "Ten");
                    commandInsert.Parameters.Add("@MoTa", SqlDbType.NVarChar, 50, "MoTa");
                    commandInsert.Parameters.Add("@SoLuongTrongKho", SqlDbType.Int, 50, "SoLuongTrongKho");
                    commandInsert.Parameters.Add("@GiaTien", SqlDbType.Money, 50, "GiaTien");
                    da.InsertCommand = commandInsert;
                    da.Update(dt);
                    MessageBox.Show("Ban da them Thanh Cong", "chuc Mung", MessageBoxButtons.OK);
                }
            }

       
        private void ThemVT_Load_1(object sender, EventArgs e)
        {
            conn.connect();
            getdata();
        }
    }
}
